package com.aj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.aj.entity.ASearchRequest;
import com.aj.entity.Article;
import com.aj.entity.Tag;
import com.aj.service.ArticleService;
import com.aj.util.Result;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aj
 * @since 2020-08-03
 */
@RestController
@CrossOrigin(origins="http://localhost:8008")
@RequestMapping("article")//此处只写了value，没写method，可支持post、get两种传参数方式
public class ArticleController {
	
	@Autowired
	ArticleService as;

	@PostMapping("getArticleList")
	public Object getArticleList(@RequestBody Tag a)
	{
		if(a==null||a.getTid()==0)
			return as.getArticleList();
		else
			return as.getArticleListByTag(a.getTid());
	}
	
	@PostMapping("search")
	public Object search(@RequestBody ASearchRequest k)
	{
		System.out.println(k.getKeyword());
		if(k.getKeyword()==null)
			return new Result(false,"抄错接口了",500,null);
		return as.search(k.getKeyword());

	}
	
	@PostMapping("addArticle")
	public Object addArticle(@RequestBody Article a)
	{
//		System.out.println(a.title+" "+a.text);
		if(a.getTitle()==null||a.getTitle().equals(""))
		{
			System.out.println("标题不能为空");
			return new Result(false,"标题不能为空",500,null);
		}
		return as.addArticle(a);
	}

	@PostMapping("updateArticle")
	public Object updateArticle(@RequestBody Article a)
	{
//		System.out.println(a.title+" "+a.text);
		return as.updateArticle(a);
	}
	
	@PostMapping("deleteArticle")
	public Object deleteArticle(@RequestBody Article a)
	{
//		System.out.println(a.title+" "+a.text);
		return as.deleteArticle(a);
	}
	
}
